// JavaScript Document
var socketNum = 0,
	usrId = 111111,
	defaultStart = 'focus',
	iosocket = io.connect('http://10.10.168.35:888');
	
//iosocket.send('open');
iosocket.on('connect', function () {
	var dataWrap = $('#dateWrap');
	iosocket.emit('login', usrId, defaultStart);
	iosocket.on('message', function(message) { console.log(message)});
	iosocket.on('calendar-content', function(html) {
		dataWrap.html(html);
		calendarManager.aTags = dataWrap.find('a');
	});
	iosocket.on('dayPlan-content', function(date, html, dayPlanObj, startNum){
		calendarContent.html(html);
		calendarManager.calendarInputWrap = $(".calendar-input-wrap");
		calendarManager.calendarInput = $(".calendar-input");
		calendarManager.uplpadButton = $(".upload-date-gray");
		calendarManager.defaultPlan[date[0].toString()+date[1]+date[2]] = dayPlanObj;
		calendarManager.addNote = [];
		calendarManager.delPlan = [];
		calendarManager.changedPlan = {};
		calendarManager.startNum = ++startNum;
		if(isEmptyObj(dayPlanObj)) {
			$(calendarManager.aTags[date[2] - 1]).attr('class', '');	
		}
		else {
			$(calendarManager.aTags[date[2] - 1]).attr('class', 'has-tip');	
		}
	});
	iosocket.on('planMessage', function(mes){
		calendarManager.titleTime.html(mes.time);
		calendarManager.planContent.html(mes.plan);
		calendarManager.remindWrap.show();
	});
	iosocket.on('cancelPlan', function(sqnm, newTime, remove) {
		var tempKey = calendarManager.curYear.toString()+calendarManager.curMonth+calendarManager.curDay,
			defaultPlan = calendarManager.defaultPlan[tempKey][sqnm];
		if(!remove) {
			defaultPlan.tag.find('i').html(newTime);
			defaultPlan.time = newTime;
		}
		else {
			calendarManager.curPlan.remove();
		}
	});  
	iosocket.on('cancelRemind', function(){
		calendarManager.remindWrap.hide();	
	});
	iosocket.on('dayNote', function(note){
		if('' === note) {
			note = '暂无日志内容。'	
		}	
		$('#contentArea').val(note);	
	});
	iosocket.on('monthNote', function(noteArr, year, month, maxDay){
		var value = ''
		for(var i = 0; i < maxDay; i++) {
			if('' == noteArr[i]) {
				continue;	
			}
			value += '----------- '+year+'/' + month + '/' + (i+1) + ' -----------' + '\n';
			value += noteArr[i];
			value += '\n';
			value += '\n';
		}
		$('#saveNote').hide();
		$('#contentArea').val(value);
		$('.note-wrap').show();		
	})
});
var date = new Date(),
	calendarManager = {
		aTags         : [],
		curDay        : date.getDate(),
		addNote       : [],
		delPlan       : [],
		curPlan       : '',
		dayNote       : {},
		curYear       : date.getFullYear(),
		curMonth      : date.getMonth() + 1,
		startNum      : 0,
		curPanel      : '',
		planHtml      : {},
		choiceNum     : '',
		defaultDay    : date.getDate(),
		monthMemory   : {},
		changedPlan   : {},
		defaultPlan   : {},
		finishMemory  : {},
		repeatManager : {},
		dayPlanMemory : {},
		dayNoteMemory : {},
		
		titleTime           : $('#titleTime'),
		curPlanDay          : $("#curPlanDay"),
		remindWrap          : $('.remind-wrap'),
		planContent         : $('#planContent'),
		uplpadButton        : $(".upload-date-gray"),
		iReapteCheck        : $(".check-cancel"),
		calendarInput       : $(".calendar-input"),
		skipCurRemind       : $(".skip-curWarn"),
		calendarTittle      : $("#calendarTittle"),
		calendarInputWrap   : $(".calendar-input-wrap"),
		calendarConfigPanel : $("#configPanel")
	},
	calendarContent = $("#contentRight"),
	calendarManagerDay = date.getFullYear().toString() + (date.getMonth()+1).toString() + date.getDate().toString();
	
$("#IdForTopXingMingImg").bind("click", function(){
	curMonth = calendarManager.curYear.toString() + calendarManager.curMonth.toString();
	$("#calendarWrap").show();
	if(undefined == calendarManager.monthMemory[curMonth]) {
		iosocket.emit('getCalendarContent', usrId, [calendarManager.curYear, calendarManager.curMonth, calendarManager.curDay]);	
	}
});

calendarContent.bind("click", function(event){
	var model, div, tag, clsName, offsetA, offsetB, text, curMonth, type = 'time',
		calendarInputWrap, calendarInput, choiceNum,
		repeatModel = '', repeatNum = '';
		
	model = '<i class="time-wrap">__:__</i><span class="content-wrap">点击添加计划内容</span><a class="log-config"></a>';
	div = document.createElement('div');
	div.className = 'per-calendar';
	div.innerHTML = model;
	event = event || window.event;
	tag = $(event.srcElement || event.target);
	clsName = tag.attr("class");
	curMonth = calendarManager.curYear.toString() + calendarManager.curMonth.toString();
	switch(clsName) {
		case "add-note" :			
			if(isEmptyObj(calendarManager.defaultPlan[curMonth + calendarManager.curDay])) {					
				$('.no-plan').remove();
			}
			div.setAttribute("data", calendarManager.startNum++)
			this.insertBefore(div, tag.parent()[0]);
			calendarManager.addNote.push(div);
			calendarManager.uplpadButton.attr("class", "upload-date-gray upload-date");
			break;
		case "log-config":				
			if('' != calendarManager.curPanel) {
				calendarManager.curPanel.attr("class", "log-config");	
			}
			offsetA = tag.offset();
			offsetB = $(this).offset();
			calendarManager.calendarConfigPanel.attr("style","top:"+(offsetA.top - offsetB.top)+"px;");
			calendarManager.calendarConfigPanel.show();
			tag.attr("class", "log-config log-config-red");
			calendarManager.curPanel = tag;
			calendarManager.curPlan = tag.parent();
			sqnm = calendarManager.curPlan.attr('data');
			curDayNoteData = calendarManager.defaultPlan[calendarManagerDay] || {}; 
			var curNoteData = curDayNoteData[sqnm] || {};
			//优先使用修改后的选择。
			if(undefined != calendarManager.changedPlan[sqnm]) {
				choiceNum = calendarManager.changedPlan[sqnm].repeatModel;
				repeatNum = calendarManager.changedPlan[sqnm].repeatNum;
			}
			if(undefined == choiceNum && '' !== curNoteData.repeatModel) {
				choiceNum = curNoteData.repeatModel;
				repeatNum = curNoteData.repeatNum;

			}
			else {
				calendarManager.skipCurRemind.hide();
			}
			calendarManager.iReapteCheck.each(function(index){
				var label, input;
				if(index === choiceNum) {
					$(this).attr("class", "check-ok");
					label = $(this).next("label");
					label.attr("class","repeat-model");
					input = label.find('input');
					input.val(repeatNum);
					calendarManager.skipCurRemind.show();
				}
				else {
					$(this).attr("class", "check-cancel");
					label = $(this).next("label");
					label.attr("class","not-repeat");
				}
			});
			if(undefined !== curDayNoteData[sqnm] && undefined != curDayNoteData[sqnm].time) { 
				time = curDayNoteData[sqnm].time.split(':');
				hour  = date.getHours();
				minute = date.getMinutes();
				if(hour > time[0] || (hour == time[0] && minute >= time[1])) {
					calendarManager.skipCurRemind.hide();
				}
			}
			calendarManager.choiceNum = choiceNum;
			break;
		case "log-config log-config-red": 			
			calendarManager.calendarConfigPanel.hide();
			calendarManager.curPanel.attr("class", "log-config");
			calendarManager.curPanel = '';
			calendarManager.curPlan = '';
			break;
		case "time-wrap":
		case "time-wrap time-repeat":
		case "content-wrap":
			_nodeDel($(this), tag);
			break;
		case "upload-date-gray upload-date":				
			_saveDel();
			break;
	}
	function _saveDel(){
		var postDate, curDel, curNode, sqnm, len, time, plan;
		postDate = {
			uid : usrId,
			day : calendarManagerDay,
			changed : {},
			added : [],
			addStartNum : 0,
			removed : [],
			yearMonthDay : [calendarManager.curYear, calendarManager.curMonth, calendarManager.curDay]	
		}

		len = calendarManager.addNote.length;
		if(0 != len) {
			for(i = 0; i < len; i++) {
				curNode = calendarManager.addNote[i];
				sqnm = curNode.getAttribute('data');
				if(undefined != calendarManager.changedPlan[sqnm] && undefined != calendarManager.changedPlan[sqnm].repeatModel) {
					repeatModel = calendarManager.changedPlan[sqnm].repeatModel;
					repeatNum = calendarManager.changedPlan[sqnm].repeatNum;
					delete calendarManager.changedPlan[sqnm].repeatModel;
					delete calendarManager.changedPlan[sqnm].repeatNum;
					if(isEmptyObj(calendarManager.changedPlan[sqnm])) {
						delete calendarManager.changedPlan[sqnm];	
					}
				}
				time = curNode.getElementsByTagName("i")[0].innerHTML;
				plan = curNode.getElementsByTagName("span")[0].innerHTML;
				if('点击添加计划内容' == plan || '__:__' == time) continue;
				postDate.added.push({
					time : curNode.getElementsByTagName("i")[0].innerHTML,
					plan : curNode.getElementsByTagName("span")[0].innerHTML,
					repeatModel : repeatModel,
					repeatNum : repeatNum
				});
			}
			postDate.addStartNum = $('.per-calendar').length - postDate.added.length;
		}
		
		if(!isEmptyObj(calendarManager.changedPlan)) {		
			for(var i in calendarManager.changedPlan) {
				postDate.changed[i] = calendarManager.changedPlan[i];
				postDate.changed[i].defaultData = calendarManager.defaultPlan[calendarManagerDay][i];
			}
		}
		for(i in calendarManager.delPlan) {
			curDel = calendarManager.delPlan[i];
			postDate.removed.push(calendarManager.defaultPlan[calendarManagerDay][curDel]._id); 
			delete calendarManager.defaultPlan[calendarManagerDay][curDel];	
		}
		calendarManager.addNote = [];
		calendarManager.delPlan = [];
		calendarManager.changedPlan = {};
		console.log(postDate);
		iosocket.emit('post', postDate);
		$(tag).attr('class', 'upload-date-gray');
		$('.log-config log-config-red').attr('class', 'log-config');
		calendarManager.calendarConfigPanel.hide();	
	}
	function _nodeDel(parent, tag) {
		var calendarInputWrap = $(".calendar-input-wrap"),
			calendarInput = $(".calendar-input"),
			offsetA = tag.offset(),
			offsetB = parent.offset(),
			dayPlan, text;
			
		calendarInputWrap.attr("style", 'top:'+(offsetA.top - offsetB.top)+"px;");
		text = tag.text();
		if('点击添加计划内容' != text) calendarInput.val(text);
		if("content-wrap" == clsName)  type = 'plan';
		calendarInputWrap.show();
		calendarInput.focus();
		calendarInput.bind("blur", function(){
			calendarInputWrap.hide();
			var val = $(this).val(),
				timeVerificationA, timeVerificationB,
				defaultPlan = calendarManager.defaultPlan,
				changedPlan = calendarManager.changedPlan;
			if(val != '点击添加计划内容' && val != text) {
				if('plan' != type) {
					//时间有效性验证
					timeVerificationA = /^(([01]?[0-9])|(2[0-3]))：[0-5]?[0-9]$/;	//中文冒号
					timeVerificationB = /^(([01]?[0-9])|(2[0-3])):[0-5]?[0-9]$/;	//英文冒号
					if(!timeVerificationB.test(val)) {
						if(!timeVerificationA.test(val)) {
							tag.text('__:__');
							alert('错误时间!');	
						}	
						else {
							tempTime = val.split('：');	//中文状态冒号
							val = tempTime[0]+':'+tempTime[1];	
							tag.text(val);
						}
					}	
				}
				tag.text(val);
				var parent = tag.parent();
				planSqnm = parent.attr("data");
				dayPlan =  defaultPlan[calendarManagerDay];
				if(undefined != dayPlan && undefined != dayPlan[planSqnm]) {							
					if(type == 'plan'){
						if(val != defaultPlan[calendarManagerDay][planSqnm].plan) {
							changedPlan[planSqnm] = changedPlan[planSqnm] || {};
							changedPlan[planSqnm].plan = val;
						}
						else{
							delete changedPlan[planSqnm].plan;
							if(undefined == changedPlan[planSqnm].time) delete changedPlan[planSqnm];
						}
					}
					else{
						if(val != defaultPlan[calendarManagerDay][planSqnm].time) {
							changedPlan[planSqnm] = changedPlan[planSqnm] || {};
							changedPlan[planSqnm].time = val;
						}
						else{
							delete changedPlan[planSqnm].time;
							if(undefined == changedPlan[planSqnm].plan) delete changedPlan[planSqnm];	
						}
					}	
				}
			}
			if('' == val && 'plan' == type) tag.text('点击添加计划时间');
			if('' == val && 'time' == type) tag.text('点击添加计划内容');
			$(this).val('');
			$(this).unbind("blur");		
			uplpadButtonState();			
		});	
	}
});

calendarManager.calendarConfigPanel.bind("click", function(event) {
	var tag, clsName, tempIndex, sqnm, defaultPlan, curChange, defaultData;
	event = event || window.event;
	tag = $(event.srcElement || event.target);
	clsName = tag.attr("class");
	if(undefined == clsName) return false;
	switch(clsName) {
		case "del-note"	:			
			calendarManager.curPlan.remove();
			$(this).hide();
			for(var i = 0, len =  calendarManager.addNote.length; i < len; i++) {			
				if(calendarManager.curPlan[0] == calendarManager.addNote[i]) {
					calendarManager.addNote.slice(i, 1);	
					if(0 == calendarManager.delPlan.length && isEmptyObj(calendarManager.changedPlan) && 0 == calendarManager.addNote.length) {
						calendarManager.uplpadButton.attr("class", "upload-date-gray");	
					}
					return;
				}	
			}
			calendarManager.delPlan.push(calendarManager.curPlan.attr("data"));
			calendarManager.uplpadButton.attr("class", "upload-date-gray upload-date");
			break;	
		case "check-cancel":		
			tag.attr("class", "check-ok");
			label = tag.next("label");
			label.attr("class", "repeat-model");
			input = label.find('input');
			calendarManager.iReapteCheck.each(function(index) {
				if(tag[0] == $(this)[0]) {
					tempIndex = index;
					return;
				}
			});
			if(tempIndex != calendarManager.choiceNum) {
				$(calendarManager.iReapteCheck[calendarManager.choiceNum]).attr("class", "check-cancel");
				$(calendarManager.iReapteCheck[calendarManager.choiceNum]).next("label").attr("class", "not-repeat");
			}
			sqnm = calendarManager.curPlan.attr('data');
			curChange = calendarManager.changedPlan[sqnm];
			value = input.val();
			calendarManager.choiceNum = tempIndex;
			defaultData = calendarManager.defaultPlan[calendarManagerDay][sqnm];
			if(undefined === defaultData) {
				if(undefined == curChange || undefined == curChange.repeatNum || '' === curChange.repeatModelRea) {
					input.val('');	
				}
				return;//新增不做判断
			}
			if(tempIndex == defaultData.repeatModel){
				if('' === value) {
					if(undefined != curChange && undefined != curChange.repeatModel) {
						delete curChange.repeatModel;
						delete curChange.repeatNum;
						if(isEmptyObj(curChange)) {
							delete calendarManager.changedPlan[sqnm];
						}
					}	
				}
				else{
					if(value == defaultData.repeatNum) {
						curChange = calendarManager.changedPlan[sqnm];
						if(undefined != curChange && undefined != curChange.repeatModel) {
							delete curChange.repeatModel;
							delete curChange.repeatNum;
							if(isEmptyObj(curChange)) {
								delete calendarManager.changedPlan[sqnm];
							}
						}
					}
					else {
						curChange = calendarManager.changedPlan[sqnm] || (calendarManager.changedPlan[sqnm]={});
						curChange.repeatModel = tempIndex;
						curChange.repeatNum = value;
					}
				}
			} 
			if(tempIndex != defaultData.repeatModel) {
				if('' !== value) {
					curChange = calendarManager.changedPlan[sqnm] || (calendarManager.changedPlan[sqnm]={});
					curChange.repeatModel = tempIndex;
					curChange.repeatNum = value;
				}
				else {
					curChange = calendarManager.changedPlan[sqnm];
					if(undefined != curChange && undefined != curChange.repeatModel) {
						delete curChange.repeatModel;
						delete curChange.repeatNum;
						if(isEmptyObj(curChange)) {
							delete calendarManager.changedPlan[sqnm];
						}
					}		
				} 
			}
			uplpadButtonState();
			break;
		case "check-ok":		   
			tag.attr("class", "check-cancel");
			tag.next("label").attr("class", "not-repeat");
			sqnm = calendarManager.curPlan.attr('data');
			calendarManager.iReapteCheck.each(function(index) {
				if(tag[0] == $(this)[0]) {
					tempIndex = index;
					return;
				}
			});
			//自己取消。
			if(tempIndex == calendarManager.choiceNum) {
				curChange = calendarManager.changedPlan[sqnm];
				if(undefined != curChange && undefined != curChange.repeatModel) {
					delete curChange.repeatModel;
					delete curChange.repeatNum;
					if(isEmptyObj(curChange)) {
						delete calendarManager.changedPlan[sqnm];
					}
				}		
			}
			calendarManager.choiceNum = '';
			uplpadButtonState();
			break;
		case "alarm-cancel":		
			tag.attr("class", "alarm-ok");
			tag.next().attr("class", "repeat-model");
			break;
		case "alarm-ok":			
			tag.attr("class", "alarm-cancel");
			tag.next().attr("class", "not-repeat");
			break;
		case "skip-curWarn":			//取消该次提醒
			_skipFunc();
			break;
	}
	function _skipFunc(){
		var sqnm, defaultData, hour;
		
		sqnm = calendarManager.curPlan.attr('data');
		defaultData = calendarManager.defaultPlan[calendarManager.curYear.toString()+calendarManager.curMonth+calendarManager.curDay][sqnm];
		curChange = calendarManager.changedPlan[sqnm] || (calendarManager.changedPlan[sqnm] = {});
		curChange.cancel = true;
		curChange.id = defaultData._id;
		curChange.sqnm = sqnm;
		curChange.time = defaultData.time;
		defaultData.tag = calendarManager.curPlan;
		uplpadButtonState();
	}
});
$(".repeat-num").bind("blur", function(){
	var val = $(this).val(),
		sqnm = calendarManager.curPlan.attr('data'),
		defaultNodeSet, curChange;	

	curChange = calendarManager.changedPlan[sqnm] || (calendarManager.changedPlan[sqnm] = {});
	calendarManager.defaultPlan[calendarManagerDay] = calendarManager.defaultPlan[calendarManagerDay] || {};
	defaultNodeSet = calendarManager.defaultPlan[calendarManagerDay][sqnm];
	if('' === val) return false;
	val = Math.floor(val);
	if(undefined == defaultNodeSet){
		curChange.repeatModel = calendarManager.choiceNum;
		curChange.repeatNum = val;
		return;
	}
	if(defaultNodeSet.repeatModel === calendarManager.choiceNum && defaultNodeSet.repeatNum === val) {
		delete calendarManager.changedPlan[sqnm].repeatModel;
		delete calendarManager.changedPlan[sqnm].repeatNum;
		if(isEmptyObj(calendarManager.changedPlan[sqnm])) {
			delete calendarManager.changedPlan[sqnm];
		}
		uplpadButtonState();
		return;
	}
	
	curChange.repeatModel = calendarManager.choiceNum;
	curChange.repeatNum = val;
	uplpadButtonState();
});
$(".content-left").bind("click", function(event){
	var tag, clsName, curMonth, day;
	event = event || window.event;
	tag = $(event.srcElement || event.target);
	clsName = tag.attr("class");
	day = tag.text();
	if('exportMonthNote' == tag.attr('id')) {
		iosocket.emit('getMonthNote', usrId, calendarManager.curYear, calendarManager.curMonth);
		return;
	}
	if(day == calendarManager.curDay) return;
	curDay = day;
	calendarManager.curDay = day;
	curMonth = calendarManager.curYear.toString() + calendarManager.curMonth.toString();
	calendarManagerDay = curMonth+day;
	if("A" == tag.context.nodeName) {
		calendarManager.curPlanDay.text(day);
		if('' == clsName) {
			html = '<div class="no-plan">该日暂无计划</div><div class="add-calendar-wrap"><a class="add-note">添加计划</a><a class="upload-date-gray">保存更新</a></div><div class="calendar-input-wrap"><input type="text" class="calendar-input" /></div>';
		calendarContent.html(html);
		calendarManager.startNum = 0;
		calendarManager.calendarInputWrap = $(".calendar-input-wrap");
		calendarManager.calendarInput = $(".calendar-input");
		calendarManager.uplpadButton = $(".upload-date-gray");
		calendarManager.defaultPlan[day] = {};
		calendarManager.addNote = [];
		calendarManager.delPlan = [];
		calendarManager.changedPlan = {};
		}
		else {
			iosocket.emit('getDayPlan', usrId, [calendarManager.curYear, calendarManager.curMonth, day]);
		}	
	}
});
$('#lastMonth').bind("click", function() {
	var tempMonth = calendarManager.curMonth, yearMonth, defaultDay;
	
	tempMonth = tempMonth - 1;
	calendarManager.curMonth = tempMonth;
	if(0 == tempMonth) {
		calendarManager.curMonth = 12;
		calendarManager.curYear -= 1;	
	}
	yearMonth = calendarManager.curYear.toString() + calendarManager.curMonth;
	defaultDay = calendarManager.defaultDay.toString();
	calendarManager.curDay = defaultDay;
	calendarManagerDay = yearMonth + defaultDay;
	iosocket.emit('getCalendarContent', usrId, [calendarManager.curYear, calendarManager.curMonth, defaultDay]); 		
	calendarManager.curPlanDay.text(defaultDay);
	calendarManager.calendarTittle.text(calendarManager.curYear+"年"+calendarManager.curMonth+'月');
});
$('#nextMonth').bind("click", function(){
	var tempMonth = calendarManager.curMonth, yearMonth, defaultDay;
	
	tempMonth = tempMonth + 1;
	calendarManager.curMonth = tempMonth;
	if(13 == tempMonth) {
		calendarManager.curMonth = 1;
		calendarManager.curYear += 1;	
	}
	yearMonth = calendarManager.curYear.toString() + calendarManager.curMonth;
	defaultDay = calendarManager.curDay.toString();
	calendarManager.curDay = defaultDay;
	calendarManagerDay = curMonth + defaultDay;
	iosocket.emit('getCalendarContent', usrId, [calendarManager.curYear, calendarManager.curMonth, defaultDay]); 		
	calendarManager.curPlanDay.text(defaultDay);
	calendarManager.calendarTittle.text(calendarManager.curYear+"年"+calendarManager.curMonth+'月');
});
$('.write-note').bind('click', function(){
	var key = calendarManager.curYear+'-'+calendarManager.curMonth+'-'+calendarManager.curDay,
		val = calendarManager.dayNote[key];

	if(undefined === val) {
		iosocket.emit('getDayNote', usrId, [calendarManager.curYear, calendarManager.curMonth, calendarManager.curDay]); 
		$('#contentArea').val('日志记录读取中，请稍等。。。');
	}	
	else {
		$('#contentArea').val(val);	
	}
	$('#saveNote').show();
	$('.note-wrap').show();
})
$('#buttonOk').bind('click', function() {
	calendarManager.remindWrap.hide();
	iosocket.emit('usrSigned', usrId);
});

$('#saveNote').bind('click', function() {
	var value = $('#contentArea').val(),
		key = calendarManager.curYear+'-'+calendarManager.curMonth+'-'+calendarManager.curDay;
		
	$('.note-wrap').hide();
	iosocket.emit('dayNote', value, usrId, [calendarManager.curYear, calendarManager.curMonth, calendarManager.curDay]);
	calendarManager.dayNote[key] = value;
});
$('#cancelNote').bind('click', function(){
	$('.note-wrap').hide();	
});

function uplpadButtonState() {
	if(0 == calendarManager.addNote.length && isEmptyObj(calendarManager.changedPlan) && 0 == calendarManager.delPlan.length) {
		calendarManager.uplpadButton.attr("class", "upload-date-gray");
	}
	else {
		calendarManager.uplpadButton.attr("class", "upload-date-gray upload-date");
	}
}
function isEmptyObj(obj){
	for(var i in obj) {
		return false;	
	}
	return true;
}
//根据页面的状态来做socket状态 ，以此来做发送消息的判断；
window.onblur = function() {
	defaultStart = 'blur';
	iosocket.emit('pageState', 'blur');
}
window.onfocus = function() {
	defaultStart = 'focus';
	iosocket.emit('pageState', 'focus');
}